using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


class ConstructBuildingAtArea : TutorialEventNode
{

    [JsonAttribute("arIdx", "格子位置")]
    public List<Rect> areaIndexList = new List<Rect>();

    public static string path = "Prefabs/Tutorial/TutorialAreaPlane";
    public static GameObject _showPrefab;
    public static GameObject showPrefab
    {
        get
        {
            if (_showPrefab == null)
            {
                _showPrefab = Resources.Load<GameObject>(path);
            }
            return _showPrefab;
        }
    }

    /// <summary>
    /// 保留设置之前得地格状态
    /// </summary>
    private List<List<MapGridState>> originalStateList;

    private List<GameObject> showPrefabList = new List<GameObject>();

    
    
    /// <summary>
    /// 取消得时候 恢复原有状态
    /// </summary>
    protected override void Cancel()
    {
        for (int i = 0; i < originalStateList.Count; i++)
        {
            var list = originalStateList[i];
            for (int j = 0; j < list.Count; j++)
            {
                MapAreaManager.Instance.SetCellStateByIndex(i, j, 1, 1, originalStateList[i][j]);
            }
        }

        for (int i = showPrefabList.Count - 1; i >= 0; i--)
        {
            GameObject.Destroy(showPrefabList[i]);
        }
        showPrefabList.Clear();
        CustomBuildController.Instance._build_Can_Construct -= OnBuildingCanConstruct;
    }

    /// <summary>
    /// 初始化时， 设置所有状态为占用。
    /// </summary>
    protected override void Init()
    {
        // 关心建筑能够合法建造状态
        CustomBuildController.Instance._build_Can_Construct += OnBuildingCanConstruct;
        MapAreaData selfArea = MapAreaManager.Instance.UserArea;
        //CustomBuildController.Instance.SetDragBuildBehaviourByAreaValume(UserManager.Instance.AreaId, buildingAreaIndex.x, buildingAreaIndex.y, 2, 2);
        var data = MapAreaManager.Instance.GetAllGridData((int)UserManager.Instance.AreaId);
        originalStateList = new List<List<MapGridState>>(data.Count);
        for (int i = 0; i < data.Count; i++)
        {
            var list = new List<MapGridState>(data[i].Count);
            for (int j = 0; j < data[i].Count; j++)
            {
                list.Add(data[i][j].GridState);
            }
            originalStateList.Add(list);
        }
        MapAreaManager.Instance.SetCellStateByIndex(0,0, selfArea.Rows, selfArea.Cols, MapGridState.Occupy);
        CustomBuildController.Instance.CheckBuildingGrid();
        SetTutorialAreaState(MapGridState.Free);
        ShowPlaneForArea();
    }

    public void ShowPlaneForArea()
    {
        for (int i = 0; i < areaIndexList.Count; i++)
        {
            var show = GameObject.Instantiate<GameObject>(showPrefab);
            show.transform.localScale = new Vector3(areaIndexList[i].height, 1, areaIndexList[i].width);
            //show.transform.position = new Vector3(show.transform.position.x - areaIndexList[i].width / 2, 0.5f, show.transform.position.z - areaIndexList[i].height / 2 );
            var v2 = MapAreaManager.Instance.UserArea.GetCenterPosByStartGrid((int)areaIndexList[i].x, (int)areaIndexList[i].y, (int)areaIndexList[i].width, (int)areaIndexList[i].height);
            show.transform.position = new Vector3(v2.x, 0.5f, v2.y);
            showPrefabList.Add(show);
        }
    }

    /// <summary>
    /// 建筑能够合法建造的事件
    /// </summary>
    /// <param name="data"></param>
    protected void OnBuildingCanConstruct()
    {
        CustomBuildController.Instance.PauseDragBuilding();
        //CustomBuildController.Instance.SetState(BuildingEditState.Levitate);
        /// 触发事件
        Shoot();
    }

    /// <summary>
    /// 设置新手引导需要得CellState状态
    /// </summary>
    /// <param name="state"></param>
    protected void SetTutorialAreaState(MapGridState state)
    {
        for (int i = 0; i < areaIndexList.Count; i++)
        {
            MapAreaManager.Instance.SetCellStateByIndex((int)areaIndexList[i].x, (int)areaIndexList[i].y, (int)areaIndexList[i].width, (int)areaIndexList[i].height, state);
        }
    }
}

